package lanqiao;

import java.util.Arrays;

/**
 * @Project : AlgorithmLearning
 * @Package : LanQiao.PrepareForCompetition
 * @File : Q1460.java
 * @Author : WangRuoyu
 * @Date : 2023/3/29 11:19
 */

public class Q1460 {
    static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    public static void main(String[] args) {
        int[][] p = new int[2022][2022];
        for (int[] ints : p) {
            Arrays.fill(ints, (int) 1e9);
        }

        for (int i = 1; i <= 2021; ++i) {
            for (int j = i; j <= 2021; ++j) {
                if (i == j) {
                    p[i][j] = 0;
                } else if (j - i <= 21) {
                    int dis = i * j / gcd(i, j);
                    p[i][j] = dis;
                    p[j][i] = dis;
                }
            }
        }
        boolean[] st = new boolean[2022];
        int[] dist = new int[2022];
        Arrays.fill(dist, (int) 1e9);
        dist[1] = 0;
        for (int i = 1; i < 2021; ++i) {
            int index = -1;
            for (int j = 1; j <= 2021; ++j) {
                if (!st[j] && (index == -1 || dist[j] < dist[index])) {
                    index = j;
                }
            }
            st[index] = true;
            for (int j = 1; j <= 2021; ++j) {
                dist[j] = Math.min(dist[j], dist[index] + p[index][j]);
            }
        }

        System.out.println(dist[2021]);
    }
}
